<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

class m_medias_actions_block_ViewImageAction extends m_backoffice_extends_Action
{
	public function execute()
	{
		$docId = $this->getRequestParameter('docId');
        $docLang = $this->getRequestParameter('docLang');
        $width = $this->getRequestParameter('width');
        $height = $this->getRequestParameter('height');

        $media = f_document_Provider::getInstance()->getByModelAndUniqueId('medias/Media', $docId, $docLang);

        $listService = m_backoffice_services_ListService::getInstance();
        $this->addToModel('nextImage', $listService->getNextDocument($media));
        $this->addToModel('previousImage', $listService->getPreviousDocument($media));

        if($media->isImage())
        {
            $fileName = $media->getFileNameWoExt();

            if($width != 'default' && $height != 'default')
            {
                $fileName = $fileName.'_'.$width.'_'.$height;
            }

            $fileName = $fileName.'.'.$media->getExtension();

            $image = $media->getFileUrl($fileName)."?".rand(0,50);

            $dimensions = $media->getDimensions();

            if(!is_null($width) && $width != 'default') { $dimensions[0] = $width; }
            
            if(!is_null($height) && $height != 'default') { $dimensions[1] = $height; }
            
            if($dimensions[0] > 700 || $dimensions[1] > 400)
            {
                $dimensions[0] = 400;
                $dimensions[1] = 400;
                $this->addToModel('originalImage', $image);
                $image = $media->getPreview();
            }

            if($dimensions[0] < 500)
            {
                $dimensions[0] = 500;
            }

            $this->addToModel('images', $this->getSizedImages($media));
            $this->addToModel('dimensions', $dimensions);
            $this->addToModel('media', $media);
            $this->addToModel('fileName', $fileName);
            $this->addToModel('image', $image);
            $this->setView('success');
        }
        else
        {
            $this->addToModel('fileName', $media->getFileNameWoExt().'.'.$media->getExtension());
            $this->addToModel('media', $media);
            $this->setView('fileSuccess');
        }

		
	}	

    private function getSizedImages($media)
    {
        /* @var $media m_medias_documents_Media */
        $images = $media->getAllSizesImagesObjects();

        $result = array();

        foreach($images as $image)
        {
            if($image->getFilename() != $media->getFileNameWoExt())
            {
                $params = explode('_', $image->getFilename());
                $length = count($params);
                $res['width'] = $params[$length-2];
                $res['height'] = $params[$length-1];
                $res['original'] = false;
            }
            else
            {
                $dimensions = $media->getDimensions();
                $res['width'] = $dimensions[0];
                $res['height'] = $dimensions[1];
                $res['original'] = true;
            }

            $res['fileName'] = $image->getFilename();
            $res['fileSize'] = $image->getFilesizeFormatted();

            $result[] = $res;
        }

        return $result;
    }
}